<template>
	<view class="container">
		<view class="empty" v-if="list.length < 1">
			<image class="image" src="@/static/img/empty.png" mode="aspectFit"></image>
			<view class="emptyWord">
				<view>无可关联设备</view>
				<view>提示：关联设备的分组与房间分组一致</view>
			</view>
		</view>
		<block v-else>
			<view class="list">
				<view class="box" hover-class="hoverClass" v-for="item in list" :key="item.lock_id" @click="bindLock(item)">
					<view class="info">
						<view class="name">
							{{ item.lock_alias }}
						</view>
						<view class="status">电量：{{ item.electric_quantity }}%</view>
					</view>
					<uni-icons type="plus" size="20" color="#2979ff"></uni-icons>
				</view>
			</view>
			<uni-pagination v-if="total > FormData.perpage" :total="total" :pageSize="FormData.perpage" :value="FormData.page" @change="changePage" />
		</block>
	</view>
</template>

<script>
import api from '@/api/api';
export default {
	data() {
		return {
			id: null,
			total: 0,
			FormData: {
				page: 1,
				perpage: 10,
				group_id: null,
				status: 'no_bind_room'
			},
			list: []
		};
	},
	onLoad(e) {
		this.id = e.id;
		this.FormData.group_id = e.group;
	},
	onShow() {
		this.getList();
	},
	onPullDownRefresh() {
		this.getList();
	},
	methods: {
		bindLock(item) {
			uni.showLoading({
				title: '绑定中...'
			});
			let data = {
				room_id: this.id,
				lock_id: item.lock_id
			};
			api.lockBind(data).then((res) => {
				if (res.data.is_success) {
					uni.hideLoading();
					uni.showToast({
						title: '绑定成功'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 1000);
				}
			});
		},
		getList() {
			api.lockList(this.FormData).then((res) => {
				this.list = res.data.items;
				this.total = res.data.total;
				uni.stopPullDownRefresh();
			});
		}
	}
};
</script>

<style lang="scss">
.list {
	padding: 20rpx;
	.box {
		background: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
		.info {
			.name {
				font-size: 28rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
			}
			.status {
				font-size: 26rpx;
				color: $uni-base-color;
			}
		}
	}
}
</style>